<script setup lang="ts">
import { ref } from "vue";
const current = ref(0);

const microSend = () => {
  window?.microApp?.dispatch({ type: "这是子应用向父应用发送的消息" }, () => {
    console.log("数据发送完成");
  });
};

window?.microApp?.addDataListener((data: any) => {
  alert("子应用收到数据\n" + JSON.stringify(data));

  return "我是子应用，我已经收到你传递的数据";
});

const sendCurrent = () => {
  window?.microApp?.setGlobalData({ current: current.value });
};

const forceSend = () => {
  current.value++
  window?.microApp?.forceSetGlobalData({ current: current.value }, () => {
    console.log("数据发送完成");
  });
};
</script>

<template>
  <div>
    <button @click="microSend">向父应用发送消息</button>
    <button @click="sendCurrent">向父应用发送消息</button>
    <button @click="forceSend">向父应用强制发送消息</button>
  </div>
</template>

<style lang="scss" scoped></style>
